package product

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/product"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var DevAssetMetadata = cProductDev_asset_metadata{}

type cProductDev_asset_metadata struct{}

// GetDevAssetMetadataList 获取列表
func (u *cProductDev_asset_metadata) GetDevAssetMetadataList(ctx context.Context, req *product.GetDevAssetMetadataListReq) (res *product.GetDevAssetMetadataListRes, err error) {
	var reqData = new(model.GetDevAssetMetadataListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.ProductDevAssetMetadata().GetDevAssetMetadataList(ctx, reqData)
	res = new(product.GetDevAssetMetadataListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetDevAssetMetadataById 获取指定ID数据
func (u *cProductDev_asset_metadata) GetDevAssetMetadataById(ctx context.Context, req *product.GetDevAssetMetadataByIdReq) (res *product.GetDevAssetMetadataByIdRes, err error) {
	data, err := service.ProductDevAssetMetadata().GetDevAssetMetadataById(ctx, req.Id)
	res = new(product.GetDevAssetMetadataByIdRes)
	gconv.Scan(data, &res)
	return
}

// GetDevAssetMetadataById 获取指定ID数据
func (u *cProductDev_asset_metadata) GetDevAssetMetadataByProductKey(ctx context.Context, req *product.GetDevAssetMetadataByProductKeyReq) (res *product.GetDevAssetMetadataByProductKeyRes, err error) {
	data, err := service.ProductDevAssetMetadata().GetDevAssetMetadataByProductKey(ctx, req.ProductKey)
	res = new(product.GetDevAssetMetadataByProductKeyRes)
	gconv.Scan(data, &res)
	return
}

// AddDevAssetMetadata 添加数据
func (u *cProductDev_asset_metadata) AddDevAssetMetadata(ctx context.Context, req *product.AddDevAssetMetadataReq) (res *product.AddDevAssetMetadataRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.AddDevAssetMetadata{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.ProductDevAssetMetadata().AddDevAssetMetadata(ctx, data)
	return
}

// EditDevAssetMetadata 修改数据
func (u *cProductDev_asset_metadata) EditDevAssetMetadata(ctx context.Context, req *product.EditDevAssetMetadataReq) (res *product.EditDevAssetMetadataRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.EditDevAssetMetadata{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.ProductDevAssetMetadata().EditDevAssetMetadata(ctx, data)
	return
}

// DeleteDevAssetMetadata 删除数据
func (u *cProductDev_asset_metadata) DeleteDevAssetMetadata(ctx context.Context, req *product.DeleteDevAssetMetadataReq) (res *product.DeleteDevAssetMetadataRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.ProductDevAssetMetadata().DeleteDevAssetMetadata(ctx, req.Ids)
	return
}
